﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using CuteRibs.WowArmory.ViewModels;

namespace CuteRibs.WowArmory.Views
{
	public partial class SearchPageView : PageBase
	{
		private SearchPageViewModel viewModel { get { return this.ViewModel as SearchPageViewModel; } }

		public SearchPageView()
		{
			InitializeComponent();

			this.Loaded += delegate { this.tbTerm.Focus(); };
		}

		protected override void OnBackKeyPress(CancelEventArgs e)
		{
			base.OnBackKeyPress(e);
			this.viewModel.OnBackKeyPress(e);
		}

		private void TextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				this.Focus();
				this.viewModel.DoSearch(this.tbTerm.Text.Trim());
			}
		}

		private void Button_Tap(object sender, EventArgs e)
		{
			this.viewModel.DoSearch(this.tbTerm.Text.Trim());
		}

		private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count > 0)
			{
				this.viewModel.NavigateTo(e.AddedItems[0] as SearchResultItemViewModel);
			}
		}

		private void btnPrevious_Click(object sender, EventArgs e)
		{
			this.viewModel.LoadPage(false);
		}

		private void btnNext_Click(object sender, EventArgs e)
		{
			this.viewModel.LoadPage(true);
		}
	}
}